﻿using System.Diagnostics;
using System.Linq;
using Xc.DataAccess.Core;
using System.Xml.Linq;
namespace Xc.DataAccess.Processes
{
    public class WindowsServiceManager : IWindowsServiceManager
    {
        public WindowsServiceManager()
        {

        }
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="servcieName"></param>
        /// <returns></returns>
        public Result Start(string servcieName)
        {
            var process = Process.Start("cmd", $"sc start {servcieName}");
            var res = process.StandardOutput.ReadLine();
            process.WaitForExit();
            if (process.ExitCode == 0)
            {
                return Result.Accept(res);
            }
            else
            {
                return Result.Reject(res);
            }
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="servcieName"></param>
        /// <returns></returns>
        public Result Stop(string servcieName)
        {
            var process = Process.Start("cmd", $"sc stop {servcieName}");
            var res = process.StandardOutput.ReadLine();
            process.WaitForExit();
            if (process.ExitCode == 0)
            {
                return Result.Accept(res);
            }
            else
            {
                return Result.Reject(res);
            }
        }
        /// <summary>
        /// 获取服务列表
        /// </summary>
        /// <returns></returns>
        public Process[] GetProcesses()
        {
           return  Process.GetProcesses();
        }
    }
}
